package com.yami.shop.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yami.shop.bean.model.Report;
import com.yami.shop.bean.model.ReportTitle;
import com.yami.shop.bean.vo.ReportDetailVO;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.common.util.StringUtils;
import com.yami.shop.security.platform.util.SecurityUtils;
import com.yami.shop.service.ReportService;
import com.yami.shop.service.ReportTitleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/report")
@AllArgsConstructor
@Tag(name = "举报")
public class ReportController {
    
    private final ReportService reportService;
    
    private final ReportTitleService reportTitleService;
    
    @GetMapping("/title/listMap")
    @Operation(summary =  "举报标题列表", description = "举报标题列表")
    public ServerResponseEntity<Map<Integer, List<ReportTitle>>> reportTitleListMap() {
        List<ReportTitle> list = reportTitleService.list(new LambdaQueryWrapper<ReportTitle>().orderByAsc(ReportTitle::getOrderNum));
        Map<Integer, List<ReportTitle>> listMap = list.stream().collect(Collectors.groupingBy(ReportTitle::getType));
        return ServerResponseEntity.success(listMap);
    }
    
    @GetMapping("/title/list")
    @Operation(summary =  "举报标题列表", description = "举报标题列表")
    public ServerResponseEntity<List<ReportTitle>> reportTitleList() {
        List<ReportTitle> list = reportTitleService.list(new LambdaQueryWrapper<ReportTitle>().orderByAsc(ReportTitle::getOrderNum));
        return ServerResponseEntity.success(list);
    }
    
    @GetMapping("/title/{id}")
    @Operation(summary =  "举报标题", description = "举报标题")
    public ServerResponseEntity<ReportTitle> getReportTitleById(@PathVariable Long id) {
        ReportTitle reportTitle = reportTitleService.getById(id);
        return ServerResponseEntity.success(reportTitle);
    }
    
    @PostMapping("/title")
    @Operation(summary =  "新增举报标题", description = "新增举报标题")
    public ServerResponseEntity<Void> addReportTitle(@RequestBody ReportTitle reportTitle) {
        ReportTitle one = reportTitleService.getOne(new LambdaQueryWrapper<ReportTitle>().eq(ReportTitle::getTitle, reportTitle.getTitle()).eq(ReportTitle::getType, reportTitle.getType()));
        if (Objects.nonNull(one)) {
            return ServerResponseEntity.showFailMsg("该标题已存在！");
        }
        reportTitleService.save(reportTitle);
        return ServerResponseEntity.success();
    }
    
    @PutMapping("/title")
    @Operation(summary =  "修改举报标题", description = "修改举报标题")
    public ServerResponseEntity<Void> updateReportTitle(@RequestBody ReportTitle reportTitle) {
        ReportTitle one = reportTitleService.getOne(new LambdaQueryWrapper<ReportTitle>().eq(ReportTitle::getTitle, reportTitle.getTitle()).ne(ReportTitle::getId, reportTitle.getId()).eq(ReportTitle::getType, reportTitle.getType()));
        if (Objects.nonNull(one)) {
            return ServerResponseEntity.showFailMsg("该标题已存在！");
        }
        reportTitleService.updateById(reportTitle);
        return ServerResponseEntity.success();
    }
    
    @DeleteMapping("/title/{id}")
    @Operation(summary =  "删除举报标题", description = "删除举报标题")
    public ServerResponseEntity<Void> delReportTitle(@PathVariable Long id) {
        reportTitleService.removeById(id);
        return ServerResponseEntity.success();
    }

    
    @GetMapping("/page")
    @Operation(summary =  "举报分页", description = "举报分页")
    public ServerResponseEntity<IPage<ReportDetailVO>> page(PageParam<Report> page, @ParameterObject ReportDetailVO param) {
        IPage<ReportDetailVO> iPage = reportService.page(page, param);
        return ServerResponseEntity.success(iPage);
    }
    
    
    @PostMapping("/audit")
    @Operation(summary =  "审核举报", description = "审核举报")
    public ServerResponseEntity audit(@RequestBody Report report) {
        Long userId = SecurityUtils.getSysUser().getUserId();
        report.setHandleUser(userId);
        reportService.handle(report);
        return ServerResponseEntity.success();
    }
    
    
    
    
    
    
    
    
    
    
    
}
